package com.szcinda.repository;

import com.szcinda.service.DateUtils;
import com.szcinda.service.dto.history.HistoryZadOrderImportDto;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.util.StringUtils;

import javax.persistence.Entity;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.Set;

@EqualsAndHashCode(callSuper = true)
@Data
@Entity
public class HistoryZadOrder extends BaseEntity {
    /**
     * 应收或者应付  IN 、OUT
     */
    private String type;
    /**
     * 所属机构
     */
    private String organizationId; // 保存时填充
    private String organizationName;
    /**
     * 订舱号
     */
    private String bookNo;
    /**
     * 客户
     */
    private String clientId; // 保存时填充数据
    private String clientName;
    /**
     * 过滤字段
     */
    private String carrierName;
    /**
     * 业务类型
     */
    private String businessType;
    /**
     * 计费方式
     */
    private String calculateType;
    /**
     * 发车日期
     */
    private LocalDate sendTime; // 日期格式 yyyy-mm-dd
    /**
     * 班列日期
     */
    private LocalDate blTime;  // 日期格式 yyyy-mm-dd
    /**
     * 始发地
     */
    private String startPlace;
    /**
     * 目的地
     */
    private String endPlace;
    /**
     * 柜号
     */
    private String packageNo;
    /**
     * 运费供应商
     */
    private String transportFeeCarrier;
    /**
     * 运费（USD）
     */
    private double transportFee1;
    /**
     * 运费（RMB）
     */
    private double transportFee2;
    /**
     * DTHC供应商
     */
    private String dthcCarrier;
    /**
     * DTHC（USD）
     */
    private double dthc;
    /**
     * 报关费(USD)
     */
    /**
     * 报关费供应商
     */
    private String bgCarrier;
    private double bgFee1;
    /**
     * 报关费(RMB)
     */
    private double bgFee2;
    /**
     * 报关费续页（RMB）
     */
    private double bgFee3;
    /**
     * 查验费供应商
     */
    private String checkCarrier;
    /**
     * 查验费(USD)
     */
    private double checkFee1;
    /**
     * 查验费(RMB)
     */
    private double checkFee2;
    /**
     * 其他费用供应商
     */
    private String otherCarrier;
    /**
     * 其他费用（USD)
     */
    private double otherFee1;
    /**
     * 其他费用（RMB)
     */
    private double otherFee2;
    /**
     * 堆存费供应商
     */
    private String dcCarrier;
    /**
     * 堆存费（USD）
     */
    private double dcFee1;
    /**
     * 堆存费（RMB）
     */
    private double dcFee2;
    /**
     * 国内费用供应商
     */
    private String domesticCarrier;
    /**
     * 国内费用(RMB)
     */
    private Double domesticFee;
    // 提箱地
    private String pickPlace;
    // 最终目的地
    private String destPlace;

    //    @ExcelProperty(value = "到站时间")
    private String arriveTime;
    //    @ExcelProperty(value = "提箱时间")
    private String pickTime;
    //    @ExcelProperty(value = "额外储存天数")
    private String keepDays;
    /**
     * 备注
     */
    private String remark;

    // 是否锁定 0 未锁定 1 锁定
    private int lockStatus = 0;

    public void updateCarrierName(){
        Set<String> names = new HashSet<>();
        if(StringUtils.hasText(this.transportFeeCarrier)){
            names.add(this.transportFeeCarrier);
        }
        if(StringUtils.hasText(this.dthcCarrier)){
            names.add(this.dthcCarrier);
        }
        if(StringUtils.hasText(this.bgCarrier)){
            names.add(this.bgCarrier);
        }
        if(StringUtils.hasText(this.checkCarrier)){
            names.add(this.checkCarrier);
        }
        if(StringUtils.hasText(this.otherCarrier)){
            names.add(this.otherCarrier);
        }
        if(StringUtils.hasText(this.dcCarrier)){
            names.add(this.dcCarrier);
        }
        this.carrierName = String.join(",", names);
    }

    public void updateDate(HistoryZadOrderImportDto importDto) {
        if (this.sendTime == null) {
            try {
                this.sendTime = DateUtils.toLocalDate(importDto.getSendTime().trim());
            } catch (Exception ignored) {
            }
        }
        if (this.blTime == null) {
            try {
                this.blTime = DateUtils.toLocalDate(importDto.getBlTime().trim());
            } catch (Exception ignored) {
            }
        }
    }

    public void updateBaseInfo(HistoryZadOrderImportDto importDto, String type) {
        if (StringUtils.isEmpty(this.organizationName)) {
            this.organizationName = importDto.getOrganizationName();
        }
        if (StringUtils.isEmpty(this.clientName)) {
            this.clientName = importDto.getClientName();
        }
        if (StringUtils.isEmpty(this.businessType)) {
            this.businessType = importDto.getBusinessType();
        }
        if (StringUtils.isEmpty(this.calculateType)) {
            this.calculateType = importDto.getCalculateType();
        }
        if (this.sendTime == null) {
            try {
                this.sendTime = DateUtils.toLocalDate(importDto.getSendTime().trim());
            } catch (Exception ignored) {
            }
        }
        if (this.blTime == null) {
            try {
                this.blTime = DateUtils.toLocalDate(importDto.getBlTime().trim());
            } catch (Exception ignored) {
            }
        }
        if (StringUtils.isEmpty(this.startPlace)) {
            this.startPlace = importDto.getStartPlace();
        }
        if (StringUtils.isEmpty(this.endPlace)) {
            this.endPlace = importDto.getEndPlace();
        }
        // 供应商直接覆盖
        if (StringUtils.hasText(importDto.getTransportFeeCarrier())) {
            this.transportFeeCarrier = importDto.getTransportFeeCarrier();
        }
        if (StringUtils.hasText(importDto.getDthcCarrier())) {
            this.dthcCarrier = importDto.getDthcCarrier();
        }
        if (StringUtils.hasText(importDto.getBgCarrier())) {
            this.bgCarrier = importDto.getBgCarrier();
        }
        if (StringUtils.hasText(importDto.getCheckCarrier())) {
            this.checkCarrier = importDto.getCheckCarrier();
        }
        if (StringUtils.hasText(importDto.getOtherCarrier())) {
            this.otherCarrier = importDto.getOtherCarrier();
        }
        if (StringUtils.hasText(importDto.getDcCarrier())) {
            this.dcCarrier = importDto.getDcCarrier();
        }
        if (StringUtils.hasText(importDto.getDomesticCarrier())) {
            this.domesticCarrier = importDto.getDomesticCarrier();
        }

        this.transportFee1 = this.transportFee1 + importDto.getTransportFee1();
        this.transportFee2 = this.transportFee2 + importDto.getTransportFee2();
        this.dthc = this.dthc + importDto.getDthc();
        this.bgFee1 = this.bgFee1 + importDto.getBgFee1();
        this.bgFee2 = this.bgFee2 + importDto.getBgFee2();
        this.bgFee3 = this.bgFee3 + importDto.getBgFee3();
        this.checkFee1 = this.checkFee1 + importDto.getCheckFee1();
        this.checkFee2 = this.checkFee2 + importDto.getCheckFee2();
        this.otherFee1 = this.otherFee1 + importDto.getOtherFee1();
        this.otherFee2 = this.otherFee2 + importDto.getOtherFee2();
        this.dcFee1 = this.dcFee1 + importDto.getDcFee1();
        this.dcFee2 = this.dcFee2 + importDto.getDcFee2();
        if(this.domesticFee == null){
            this.domesticFee = 0d;
        }
        this.domesticFee = this.domesticFee + importDto.getDomesticFee();
        if (StringUtils.hasText(importDto.getArriveTime())) {
            this.arriveTime = importDto.getArriveTime();
        }
        if (StringUtils.hasText(importDto.getPickTime())) {
            this.pickTime = importDto.getPickTime();
        }
        if (StringUtils.hasText(importDto.getKeepDays())) {
            this.keepDays = importDto.getKeepDays();
        }
        if (StringUtils.hasText(importDto.getRemark())) {
            if (StringUtils.hasText(this.remark)) {
                this.remark = this.remark + ";" + importDto.getRemark();
            } else {
                this.remark = importDto.getRemark();
            }
        }
        if(StringUtils.isEmpty(this.pickPlace)){
            this.pickPlace = importDto.getPickPlace();
        }
        if(StringUtils.isEmpty(this.destPlace)){
            this.destPlace = importDto.getDestPlace();
        }
    }
}
